#!/bin/sh -x
# shell script to upload to Launchpad PPA
#
# somewhat clunky as it written to run on something that isn't a debian system...
#

LAST_VERSION=`cat ~/.pidgin-musictracker-release-version`
LAUNCHPAD_PUBLIC_KEY=0x7633C10E
SV=$LAST_VERSION+$SNAPSHOT

##
## build the source tarball
##

# unpack
tar -jxf pidgin-musictracker-snapshot-$SNAPSHOT.tar.bz2

# repack with name in expected format for debian package
# repack as .gz not .bz2 as the build infrastructure doesn't support .bz2 atm...
mv pidgin-musictracker-snapshot pidgin-musictracker_${SV}
tar -zcf pidgin-musictracker_${SV}.orig.tar.gz pidgin-musictracker_${SV}

# apply debianization patch by Craig Small <csmall@debian.org>
(cd pidgin-musictracker_${SV} ; cat packaging/pidgin-musictracker-debianize.diff | patch -p1 )

##
## do launchpad build for given release
##

SRC_OPTION=sa

launchpad_build()
{
PV=$SV-0~ppa1~${RELEASE}1

# rewrite the changelog
# set the version, identify myself as the uploader, say this is snapshot
echo "pidgin-musictracker (${PV}) $RELEASE; urgency=low" >pidgin-musictracker_${SV}/debian/changelog
echo "" >>pidgin-musictracker_${SV}/debian/changelog
echo "  * Snapshot $SNAPSHOT" >>pidgin-musictracker_${SV}/debian/changelog
echo "" >>pidgin-musictracker_${SV}/debian/changelog
echo "" >>pidgin-musictracker_${SV}/debian/changelog
echo " -- Jon TURNEY <jon.turney@dronecode.org.uk>  `date -R`" >>pidgin-musictracker_${SV}/debian/changelog
echo >>pidgin-musictracker_${SV}/debian/changelog

# build source package
dpkg-source -b  -sP pidgin-musictracker_${SV} pidgin-musictracker_${SV}.orig.tar.gz

# sign .dsc file
echo >>pidgin-musictracker_${PV}.dsc
gpg --default-key $LAUNCHPAD_PUBLIC_KEY --clearsign pidgin-musictracker_${PV}.dsc
mv pidgin-musictracker_${PV}.dsc.asc pidgin-musictracker_${PV}.dsc

# build .changes files
(cd pidgin-musictracker_${SV} ; dpkg-genchanges -S -${SRC_OPTION} >../pidgin-musictracker_${PV}_source.changes )

# only include original source tarball in the first source package
# (we're not allowed to upload the same file twice)
SRC_OPTION=sd

# sign .changes file
echo >>pidgin-musictracker_${PV}_source.changes
gpg --default-key $LAUNCHPAD_PUBLIC_KEY --clearsign pidgin-musictracker_${PV}_source.changes
mv pidgin-musictracker_${PV}_source.changes.asc pidgin-musictracker_${PV}_source.changes

# upload to my launchpad PPA
dput my-ppa pidgin-musictracker_${PV}_source.changes

# clean up
rm -rf pidgin-musictracker_${PV}.diff.gz
rm -rf pidgin-musictracker_${PV}.dsc
rm -rf pidgin-musictracker_${PV}_source.changes
}

# request a launchpad build against all active ubuntu releases
for RELEASE in hardy jaunty karmic lucid
do
 launchpad_build
done

# clean up
rm -rf pidgin-musictracker_${SV}*
